const { ipcMain } = require("electron");
const OSS = require("ali-oss");

async function connect({ endpoint, keyid, keysecret, cname, bucket, prefix }) {
  const client = new OSS({
    endpoint,
    accessKeyId: keyid,
    accessKeySecret: keysecret,
    bucket,
    cname,
  });
  try {
    const result = await client.get(prefix);
    if (result.res.statusCode === 200) {
      return { status: 0, message: "登录成功" };
    }

    return { status: 1, message: "请检查信息输入是否正确" };
  } catch (error) {
    return { status: 2, message: error.code || "访问日志文件失败" };
  }
}

ipcMain.on("connect", async (event, params = {}) => {
  event.returnValue = await connect(params);
});
